package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.CoinTypeList;
import com.chain.BrotherExchange.data.model.CountryCodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择地区/币种
 */
public class SelectedItemViewModel extends AndroidViewModel {

    private MutableLiveData<Resource<List<CoinTypeList.DataBean.ListBean>>> mCoinTypeLiveData;
    private MutableLiveData<Resource<List<CountryCodeList.DataBean.ListBean>>> mCountryCodeLiveData;

    public SelectedItemViewModel(@NonNull Application application) {
        super(application);
        mCoinTypeLiveData = new MutableLiveData<>();
        mCountryCodeLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<List<CountryCodeList.DataBean.ListBean>>> countryCode() {
        mCountryCodeLiveData.setValue(Resource.loading());

        DataRepository.get().countryCodeList(result -> {
            CountryCodeList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mCountryCodeLiveData.setValue(Resource.success(resultData.getList()));
            } else if (result.isNotNetwork()) {
                mCountryCodeLiveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                mCountryCodeLiveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                mCountryCodeLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mCountryCodeLiveData;
    }

    public List<String> convertCountryToStrings(List<CountryCodeList.DataBean.ListBean> countryList) {
        ArrayList<String> strings = new ArrayList<>();
        for (CountryCodeList.DataBean.ListBean listBean : countryList) {
            strings.add(listBean.getZh());
        }
        return strings;
    }

    public MutableLiveData<Resource<List<CoinTypeList.DataBean.ListBean>>> allCoinType() {
        mCoinTypeLiveData.setValue(Resource.loading());

        DataRepository.get().allCoinTypeList(result -> {
            CoinTypeList.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                mCoinTypeLiveData.setValue(Resource.success(resultData.getList()));
            } else if (result.isNotNetwork()) {
                mCoinTypeLiveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                mCoinTypeLiveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                mCoinTypeLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mCoinTypeLiveData;
    }

    public List<String> convertCoinTypeToStrings(List<CoinTypeList.DataBean.ListBean> countryList) {
        ArrayList<String> strings = new ArrayList<>();
        for (CoinTypeList.DataBean.ListBean listBean : countryList) {
            strings.add(listBean.getMark());
        }
        return strings;
    }
}
